package com.vitalfit.controller;


import com.vitalfit.enums.ResultEnum;
import com.vitalfit.model.dto.HandleDTO;
import com.vitalfit.model.entity.report.Report;
import com.vitalfit.model.vo.ResultVO;
import com.vitalfit.service.serviceImpl.ShopManagerServiceImpl;
import com.vitalfit.strategy.UserStrategy;
import com.vitalfit.strategy.factory.UserStrategyFactory;
import com.vitalfit.utils.ResultVOUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/managers")
@Validated
public class ShopManagerController {

    private final ShopManagerServiceImpl shopManagerService;
    private final UserStrategyFactory userStrategyFactory;

    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @GetMapping("/reports")
    public ResponseEntity<List<Report>> viewReports() {
        // 查看报告
        return ResponseEntity.ok().body(shopManagerService.getReports());
    }
    @PreAuthorize("hasRole('ROLE_SHOPMANAGER')")
    @PostMapping
    public ResultVO<Object> handleUser(@RequestBody @Valid HandleDTO handleDTO) {
        UserStrategy strategy = userStrategyFactory.getStrategy
                (handleDTO.getUserType(), handleDTO.getAction());
        if (strategy == null) {
            return ResultVOUtil.buildResult(ResultEnum.LOGIN_FAILURE,"Invalid user type or action");
        }
        return ResultVOUtil.buildResult(ResultEnum.LOGIN_SUCCESS,strategy.execute(handleDTO));
    }



}
